package ee.ut.test.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import ee.ut.test.R;
import ee.ut.test.model.ResultListItem;

public class ResultListAdapter extends ArrayAdapter<ResultListItem>{

	private List<ResultListItem> items;
	private Context context;
	
	public ResultListAdapter(Context context, List<ResultListItem> items){
		super(context, R.layout.result_list_row, items);
		this.context = context;
		this.items = items;
	}
	
	static class ViewHolder {
		protected TextView formattedFirstRow;
		protected TextView formattedSecondRow;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflator.inflate(R.layout.result_list_row, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.formattedFirstRow = (TextView) view.findViewById(R.id.formattedFirstRow);
			viewHolder.formattedSecondRow = (TextView) view.findViewById(R.id.formattedSecondRow);
			view.setTag(viewHolder);
		} else {
			view = convertView;
		}
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.formattedFirstRow.setText(items.get(position).getFormattedFirstRow());
		holder.formattedSecondRow.setText(items.get(position).getFormattedSecondRow());
		return view;
	}
}
